<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>手写sleep函数</title>
</head>
<body>
<script>
// sleep 休息一会
// then
function sleep(n) {
  let p;
   p = new Promise(reject => {
    setTimeout(() => {
      // pending 等待
      console.log(p,'///');
      reject();
      // fulfilled 成功
      // rejected  失败
      // resolve();
      console.log(p);
    },n);
  });
  return p;
}

sleep(3000)
  .then(() => {
    console.log('///');
  })
  .catch(err => {
    console.log(err);
  })
.finally(() => {
  // promise 状态从 pending 到了 fullfilled 或 rejected
  console.log('finally');
})
</script>
</body>
</html>